package com.example.tianxi.yuexiuhhpatient;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.tianxi.yuexiuhhpatient.fragment.DoctorFragment;
import com.example.tianxi.yuexiuhhpatient.fragment.HistoryFragment;
import com.example.tianxi.yuexiuhhpatient.fragment.HomeFragment;
import com.example.tianxi.yuexiuhhpatient.fragment.SettingFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener {


    protected static final String TAG = "MainActivity";
    FragmentManager fragmentManager = getSupportFragmentManager();

//    ScheduleFragment scheduleFragment;
//    PatientFragment patientFragment;
//    ChatAllHistoryFragment chatAllHistoryFragment;
    HistoryFragment historyFragment;
    SettingFragment settingFragment;
    DoctorFragment doctorFragment;
    HomeFragment homeFragment;


    private ImageView[] mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView(){

        historyFragment = HistoryFragment.newInstance();
        settingFragment = SettingFragment.newInstance();
        doctorFragment = DoctorFragment.newInstance();
        homeFragment = HomeFragment.newInstance();

        showDefaultFragment();

        LinearLayout scheduleTextView = (LinearLayout)findViewById(R.id.home_tab);
        LinearLayout historyTextView = (LinearLayout)findViewById(R.id.history_tab);
        LinearLayout patienTextView = (LinearLayout)findViewById(R.id.doctor_tab);
        LinearLayout msgTextView = (LinearLayout)findViewById(R.id.msg_tab);
        LinearLayout settingTextView = (LinearLayout)findViewById(R.id.setting_tab);

        mButton = new ImageView[5];
        mButton[0] = (ImageView)findViewById(R.id.img_schedule);
        mButton[1] = (ImageView)findViewById(R.id.img_friend);
        mButton[2] = (ImageView)findViewById(R.id.img_msg);
        mButton[3] = (ImageView)findViewById(R.id.img_setting);
        mButton[4] = (ImageView)findViewById(R.id.img_history);

        mButton[0].setOnClickListener(this);
        mButton[1].setOnClickListener(this);
        mButton[2].setOnClickListener(this);
        mButton[3].setOnClickListener(this);
        mButton[4].setOnClickListener(this);

        scheduleTextView.setOnClickListener(this);
        historyTextView.setOnClickListener(this);
        patienTextView.setOnClickListener(this);
        msgTextView.setOnClickListener(this);
        settingTextView.setOnClickListener(this);

    }

    private void showDefaultFragment() {
        switchFragment(R.id.fragment_container_MainActivity, homeFragment, HomeFragment.TAG);

    }

    private void switchFragment(int id, Fragment fragment,String tag) {
//        mButton[currentTabIndex].setSelected(false);
//        // 把当前tab设为选中状态
//        mButton[index].setSelected(true);
//        currentTabIndex = index;
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(id, fragment, tag);
        transaction.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
      //  getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_schedule:
            case R.id.home_tab:
                switchFragment(R.id.fragment_container_MainActivity, homeFragment, HomeFragment.TAG);
                break;
            case R.id.img_history:
            case R.id.history_tab:
                switchFragment(R.id.fragment_container_MainActivity, historyFragment, HistoryFragment.TAG);
                break;

            case R.id.img_friend:
            case R.id.doctor_tab:
                switchFragment(R.id.fragment_container_MainActivity, doctorFragment, DoctorFragment.TAG);
                break;

            case R.id.img_msg:
            case R.id.msg_tab:

                break;

            case R.id.img_setting:
            case R.id.setting_tab:
                switchFragment(R.id.fragment_container_MainActivity, settingFragment, SettingFragment.TAG);
                break;
            default:
                break;
        }
    }
}
